#include "BookOpDialog.h"

BookOpDialog::BookOpDialog(User user, QString book_id)
{
    this->user = user;
    this->book_id = book_id;

    layout_main = new QGridLayout;

    label_msg = new QLabel();

    but_ok = new QPushButton;
    but_no = new QPushButton;
}

BookOpDialog::~BookOpDialog()
{
    delete but_no;
    delete but_ok;

    delete label_msg;

    delete layout_main;
}

void BookOpDialog::init_view_main()
{
    setFixedSize(240, 160);

    layout_main->setSpacing(10);    //设置控件距离
    setLayout(layout_main);

    if(user.get_authority() == 0)
    {
        setWindowTitle("删除");
        label_msg->setText("是否删除图书？");
    }
    else
    {
        setWindowTitle("订阅");
        label_msg->setText("是否订阅图书？");
    }
    label_msg->setAlignment(Qt::AlignCenter);
    layout_main->addWidget(label_msg, 0, 0, 1, 2);

    but_ok->setText("是");
    QObject::connect(but_ok,&QPushButton::clicked,but_ok,[&](){
        SQLHelper sql_helper;
        sql_helper.connect();
        if(user.get_authority() == 0)
        {
            sql_helper.delete_table("books", "id='" + book_id + "';");
        }
        else
        {
            QVector<QString> v_subscribe;
            time_t now = time(0);
            v_subscribe.push_back(QString::number(now % 1000000)); // id
            v_subscribe.push_back(QString::number(user.get_id()));  //user_id
            v_subscribe.push_back(book_id);  //book_id
            v_subscribe.push_back(QDateTime::currentDateTime().toString("yyyy-MM-dd:hh:mm:ss")); //creat_time
            sql_helper.insert_table("subscribe",&v_subscribe);
        }
        delete this;
    }); //设置监听
    layout_main->addWidget(but_ok, 1, 0);

    but_no->setText("否");
    QObject::connect(but_no,&QPushButton::clicked,but_no,[&](){
        delete this;
    }); //设置监听
    layout_main->addWidget(but_no, 1, 1);
}


